﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;

namespace Buccontrols
{
    public static class Options
    {
        #region Initialize

        public static void Initialize()
        {
            rescNames = new List<string>();
            rescContent = new List<object>();
            shownCommentTypes = new List<string>();
            debugmode = false;
        }

        #endregion

        #region Private attributes

        static Font font;
        static String layoutFolder;
        static String pluginsFolder;
        static String startupPlugin;
        static List<object> rescContent;
        static List<string> rescNames;
        static List<string> shownCommentTypes;
        static bool debugmode;

        #endregion

        #region Public attributes

        public static Font Font
        {
            get { return font; }
            set { font = value; }
        }

        public static String LayoutFolder
        {
            get { return layoutFolder + "\\"; }
            set { layoutFolder = value; }
        }

        public static string PluginsFolder
        {
            get { return pluginsFolder + "\\"; }
            set { pluginsFolder = value; }
        }

        public static String StartupPluginName
        {
            get { return startupPlugin; }
            set { startupPlugin = value; }
        }

        public static List<string> ShownCommentTypes
        {
            get { return shownCommentTypes; }
            set { shownCommentTypes = value; }
        }

        #endregion

        #region Private Functions

        #endregion

        #region Public Functions

        public static void LoadImage(string name, string path)
        {
            Options.WriteComment("(i) Loading image " + name);
            rescNames.Add(name);
            rescContent.Add(Image.FromFile(path));
        }

        public static void AddResource(string name, object resc)
        {
            rescNames.Add(name);
            rescContent.Add(resc);
        }

        public static object GetResource(string name)
        {
            int index = rescNames.IndexOf(name);
            if (index == -1)
            {
                Options.WriteComment("(!) Resource not found!");
                return null;
            }

            try
            {
                Options.WriteComment("(d) returned resource: " + name);
                return rescContent[index];
            }
            catch (Exception)
            {
                Options.WriteComment("(!) Something went wrong!");
            }
            return null;
        }

        public static void LoadImageDirectory(string directory)
        {
            FileInfo[] files = (new DirectoryInfo(directory)).GetFiles();
            foreach (FileInfo file in files)
            {
                LoadImage(file.Name.Split('.')[0], file.FullName);
            }
        }

        public static bool WriteComment(string info)
        {
            try
            {
                if (info.Length > 3 && shownCommentTypes.Contains(info.Substring(0, 4)))
                    Console.WriteLine(info);
                else if (debugmode)
                    Debug.WriteLine(info);
                else return false;
                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("(*) " + info);
                return false;
            }
        }

        #endregion
    }
}
